package com.bjpowernode.controller;
import com.bjpowernode.beans.ActivitiesRemark;
import com.bjpowernode.beans.Activity;
import com.bjpowernode.beans.User;
import com.bjpowernode.exception.MyDateException;
import com.bjpowernode.exception.MyLengthException;
import com.bjpowernode.services.ActivitiesRemarkServices;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.sql.SQLException;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.UUID;

@RestController
@RequestMapping("/ActivitiesRemark")
public class ActivitiesRemarkController {
    @Autowired
    ActivitiesRemarkServices activitiesRemarkServices;


    @RequestMapping("add.action")
    public HashMap<String, Object> add(HttpServletRequest res, ActivitiesRemark activitiesRemark) throws SQLException, MyLengthException, MyDateException {
        UUID uuid = UUID.randomUUID();
        activitiesRemark.setId(uuid.toString());

        HashMap<String, Object> map = new HashMap<>();

        if (activitiesRemark.getNoteContent().length() > 250 || activitiesRemark.getNoteContent().length()<1) {
            throw new MyLengthException("名称的长度1-250");
        }

        //创建的时间
        LocalDateTime now=LocalDateTime.now();
        DateTimeFormatter dateTimeFormatterSSS=DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        String nowTime = dateTimeFormatterSSS.format(now);
        activitiesRemark.setNoteTime(nowTime);

        //创建人
        HttpSession httpSession = res.getSession();
        User user =(User) httpSession.getAttribute("user");
        activitiesRemark.setNotePerson(user.getLoginAct());

        try {
            int add = activitiesRemarkServices.add(activitiesRemark);
            if (add > 0) {
                map.put("result", "success");
            } else {
                throw new SQLException("由于业务逻辑错误，则数据库添加影响行数异常");
            }
        } catch (Exception exception) {
            exception.printStackTrace();
            throw new SQLException("由于SQL语句错误，则我们需要再次抛出一个异常交给增强控制器，数据库添加影响行数异常");
        }
        return map;
    }

    @RequestMapping("del.action")
    public HashMap<String, Object> del(String id) throws  SQLException {
        HashMap<String, Object> map = new HashMap<>();
        try {
            //mybatis会自己报SQL异常
            int del = activitiesRemarkServices.delSingleton(id);
            //SQL语法不会错误，只是影响行数的错误（比如删除的没有的ID）
            if (del > 0) {
                map.put("result", "success");
            } else {
                throw new SQLException("由于业务逻辑错误，则数据库删除影响行数异常");
            }
        } catch (Exception exception) {
            throw new SQLException("由于SQL语句错误，则我们需要再次抛出一个异常交给增强控制器，数据库删除影响行数异常");
        }
        return map;
    }


}
